/*
 * #%L
 * java model
 * %%
 * Copyright (C) 2009 - 2011 prefabware.com Stefan Isele
 * %%
 * All Rights Reserved.
 * This program is dual-licensed under the GNU General Public license
 * or a commercial license.
 * You may select to use one or the other of these licenses.
 * To obtain a commercial license please contact Stefan Isele at www.stefan-isele.de
 * Any modifications to this file must keep this entire header intact.
 * Redistribution, except as permitted by the selected license, is prohibited.
 * #L%
 */
package com.prefabware.jmodel.expression.literal;

import java.util.ArrayList;
import java.util.List;

import com.prefabware.commons.StringUtil;
import com.prefabware.jmodel.code.JCodeOptions;

public class JArrayLiteral extends JLiteralBase<Object[]> {

	public JArrayLiteral(Object[] object, Class<?> type) {
		super(object);
	}
	@Override
	public String asCode(JCodeOptions options) {
		if (this.getObject() == null) {
			return null;
		}
		List<String> asCodeList = new ArrayList<String>();
		for (Object value : this.getObject()) {
			asCodeList.add(JLiteralBase.create(value).asCode(options));
		}
		if (asCodeList.size() == 1) {
			// only one element ? avoid unnecessary brackets
			return asCodeList.get(0);
		} else {
			return "{ " + StringUtil.getListAsString(asCodeList, ",") + " }";
		}
	}


	/**
	 * @param value
	 * @return the type to use for the array-element
	 */
//	public Class<?> getElementType(Object value) {
//		if (value != null) {
//			// if possible take the type of the value
//			return value.getClass();
//		} else {
//			// if not take the component type of the array
//			return (Class<?>) getObject().getClass().getComponentType();
//		}
//	}
}
